package view;

import java.util.Timer;
import java.util.TimerTask;


import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;




// 


public class TextViewMarquee extends TextView implements android.view.ViewTreeObserver.OnPreDrawListener{

	
	
	TextViewMarquee view = this;
	Timer mTimer = new Timer();
	
	
	String context = "";
	int showContent_horizontal = 0;
	
	private boolean haveNotice = true;
	private int scroolTime = 50;   //滚动的
	
	private TextPaint paint_txt ;
	
	
	
	// 
	float x = 0;
	float y = 0;
	
	

	public TextViewMarquee(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		init();
	}
	
	public TextViewMarquee(Context context, AttributeSet attrs){
		super(context, attrs);
		
		/*
		  if (null != attrs) {
	            TypedArray ta = context.obtainStyledAttributes(attrs, );

	            if (ta != null) {
	            	
	      
	            	
	                ta.recycle();
	            }
	        }
	        else{
	        	
	        }
		  */
		
		init();
	}
	
	
	
	void init(){
		
		
		
		context = getText().toString();  // 得到文本
		
		paint_txt = getPaint();
		
		
		/*
        final Resources res = getResources();
        
		 paint_txt = new TextPaint(Paint.ANTI_ALIAS_FLAG);
		 paint_txt.density = res.getDisplayMetrics().density;
		 paint_txt.setTextSize( 16 * paint_txt.density );
		*/ 
		
		 
		 
		 
		 getViewTreeObserver().addOnPreDrawListener(this);
		 
		
	}
	
 
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		//super.onDraw(canvas);   // 不需要父控件显示出文字内容
		
		
		
		canvas.drawText(context, x, y, paint_txt);
		
		
		
		
	}
	
	

	
	 
	class TimerTaskHorizontal extends TimerTask
	{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			int width = view.getWidth();

			
			
			for(;Math.abs(x)<width; x--){    //偏移x
				
				if(x <= width/2){ //如果
					
				}
				
				try {
					Thread.sleep(scroolTime);  // 使移动的速度，慢下来
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				handler.sendEmptyMessage(1);  //刷新
				
			}
			
			
			//if (showContent_horizontal >= width) 
			{
				handler_horizontal(); // 开启新的循环
				
				// 使变量复原
				x = 0;
				showContent_horizontal = 0;
			}
			
		}
		
	};

	
	public void handler_horizontal() {	
		
		if (haveNotice) {
			
			mTimer = new Timer();
			mTimer.schedule(new TimerTaskHorizontal(), 50);  //50ms后开始执行
		}
	}
	
	
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			invalidate();
		};
	};



	// 首次对文本进行处理形成text+"   "+text的形式
	@Override
	public boolean onPreDraw() {
		// TODO Auto-generated method stub
	
		int width = view.getMeasuredWidth();
		int height = view.getMeasuredHeight();
		 
		
		StringBuilder sb = new StringBuilder();
		sb.append(context);
		for(;;){
			float width_with_white = paint_txt.measureText(sb.toString());
			if(width_with_white >= width){
				break;
			}
			
			sb.append(" ");
			
		}
		
		context = sb.toString() + context;
		
		/*
		// 等于控件长度减去文字长度,要加入的空白字符
		int width_white = width - (int)(paint_txt.measureText(context)+0.5f);
		
		StringBuilder sb = new StringBuilder();
		for(int i =0; i<width_white; i++){
			sb.append(" ");
		}
		context = context + sb.toString() + context;
		*/
		
		
		Rect bounds = new Rect();
		paint_txt.getTextBounds(context, 0, context.length(), bounds);
		//y = bounds.height(); // 
		y = height/2 + bounds.height()/2;  // 在空间中居中显示  
		
		
		handler_horizontal();

		view.getViewTreeObserver().removeOnPreDrawListener(view); //首次测量后不需要再监听了；
		
		return true;
		
	}
	

}
